<template>
  <div class="table-box">
    <div class="table-search">
      <el-form :inline="true" :model="form">
        <el-form-item label="用户UID">
          <el-input v-model="form.uid" placeholder="	用户UID" />
        </el-form-item>
        <el-form-item label="NFT-ID">
          <el-input v-model="form.nft_id" placeholder="NFT-ID" />
        </el-form-item>

        <el-form-item label="是否同步">
          <el-select v-model="form.is_sync" placeholder="是否同步">
            <el-option
              v-for="item in syncList"
              :key="item.type"
              :label="item.name"
              :value="item.type"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="直播是否上架">
          <el-select v-model="form.live_status" placeholder="直播是否上架">
            <el-option
              v-for="item in liveStatus"
              :key="item.type"
              :label="item.name"
              :value="item.type"
            >
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="onQuery">查询</el-button>
          <el-button @click="onReset">重置</el-button>
          <el-button type="primary" plain @click="onFilesSync"
            >同步礼物状态</el-button
          >
          <el-button type="primary" plain @click="onExport">导出</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="card content-box">
      <div class="table">
        <el-table :data="tableData" border v-loading="loading">
          <el-table-column prop="id" label="ID" align="center" />
          <el-table-column prop="uid" label="用户UID" align="center" />
          <el-table-column prop="nft_id" label="NFT-ID" align="center" />
          <el-table-column label="图片" align="center" width="100">
            <template #default="scope">
              <el-image
                style="width: 70px; height: 70px"
                :src="scope.row.image"
                alt=""
                fit="fill"
                :preview-src-list="[scope.row.image]"
                :preview-teleported="true"
              ></el-image>
            </template>
          </el-table-column>
          <el-table-column prop="video_file" label="视频文件" align="center" />
          <el-table-column prop="zip_file" label="压缩包文件" align="center" />
          <el-table-column prop="audio_file" label="音频文件" align="center" />
          <el-table-column prop="request_id" label="请求标识" align="center" />
          <el-table-column prop="task_id" label="直播TaskID" align="center" />
          <el-table-column
            prop="is_sync"
            label="直播是否同步"
            align="center"
            :formatter="formatter"
          />
          <el-table-column
            prop="is_delete"
            label="是否删除"
            align="center"
            :formatter="formatter"
          />
          <el-table-column
            prop="live_status"
            label="直播上架状态"
            align="center"
            :formatter="formatter"
          />
          <el-table-column prop="priority" label="排序" align="center" />
          <el-table-column
            label="操作"
            align="center"
            width="380"
            fixed="right"
          >
            <template #default="scope">
              <div class="flex">
                <el-button
                  type="primary"
                  plain
                  @click="handleOperate(scope.$index, scope.row, 'add')"
                  >添加礼物</el-button
                >
                <el-button
                  type="danger"
                  @click="handleOperate(scope.$index, scope.row, 'delete')"
                  >删除礼物</el-button
                >
                <el-button
                  type="primary"
                  plain
                  @click="handleOperate(scope.$index, scope.row, 'sort')"
                  >礼物排序上下架</el-button
                >
              </div>
            </template>
          </el-table-column>
        </el-table>
        <div class="table-pagination">
          <el-pagination
            :current-page="pageable.pageNum"
            :page-size="pageable.pageSize"
            :page-sizes="[10, 25, 50, 100]"
            :background="true"
            layout="total, sizes, prev, pager, next, jumper"
            :total="pageable.total"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          ></el-pagination>
        </div>
      </div>
    </div>
    <DialogForm
      @cancel="cancel"
      @closeDialog="closeDialog"
      @submitForm="submit"
      :dialogFormVisible="dialogVisible"
      :dialogForm="dialogForm"
      :btnLoading="btnLoading"
    />
  </div>
</template>

<script setup>
import { ref, onMounted, reactive } from "vue";
import { exportToExcel, formatter } from "@/utils/utils";
import {
  nftFilesApi,
  filesSyncApi,
  filesSortApi,
  filesAddApi,
  filesDelApi,
} from "@/api/moby/nft/nft";
import DialogForm from "./components/dialogForm.vue";
const loading = ref(false);
const tableData = ref([]);
const btnLoading = ref(false);
const dialogVisible = ref(false);
// 是否同步
const syncList = [
  { type: 0, name: "否" },
  { type: 1, name: "是" },
];
// 直播是否上架
const liveStatus = [
  { type: 0, name: "未上架" },
  { type: 1, name: "已上架" },
];

const formData = {
  uid: "",
  nft_id: "",
  is_sync: "",
  live_status: "",
};
const form = reactive({ ...formData });
const dialogFormData = {
  issue_id: "",
  sort: "",
  status: "",
};
const dialogForm = reactive({ ...dialogFormData });
const pageable = reactive({
  pageNum: 1,
  pageSize: 10,
  total: 0,
});

// 参数
const param = () => {
  return {
    page: pageable.pageNum,
    pagesize: pageable.pageSize,
    nft_id: form.nft_id,
    uid: form.uid,
    is_sync: form.is_sync,
    live_status: form.live_status,
  };
};

// 获取表格数据
const getTbaleData = async () => {
  loading.value = true;

  try {
    const res = await nftFilesApi(param());
    loading.value = false;
    tableData.value = res.data.results;
    pageable.total = res.data.count;
  } catch (error) {
    loading.value = false;
  }
};

// 重置
const onReset = () => {
  Object.assign(form, formData);
};
// 查询
const onQuery = async () => {
  getTbaleData();
};
// 同步礼物状态
const onFilesSync = async () => {
  try {
    const res = await filesSyncApi({});
    ElMessage.success(res.message);

    getTbaleData();
  } catch (error) {}
};

const handleOperate = (index, row, type) => {
  // 添加
  if (type === "add") {
    ElMessageBox.confirm("您是否确认添加此礼物?", "温馨提示", {
      type: "success",
    }).then(async () => {
      const res = await filesAddApi({ issue_id: row.id });
      getTbaleData();
      ElMessage.success(res.message);
    });
  }
  // // 删除
  if (type === "delete") {
    ElMessageBox.confirm("您是否确认删除此礼物?", "温馨提示", {
      type: "warning",
    }).then(async () => {
      const res = await filesDelApi({ issue_id: row.id });
      getTbaleData();
      ElMessage.success(res.message);
    });
  }
  if (type === "sort") {
    dialogVisible.value = true;
    Object.assign(dialogForm, {
      issue_id: row.id,
    });
  }
};
//关闭弹窗
const closeDialog = () => {
  dialogVisible.value = false;
  Object.assign(dialogForm, dialogFormData);
};

// 取消
const cancel = () => {
  dialogVisible.value = false;
  Object.assign(dialogForm, dialogFormData);
};
// 提交
const submit = async (param) => {
  try {
    btnLoading.value = true;
    const res = await filesSortApi(param);
    ElMessage({
      showClose: true,
      message: res.message,
      type: "success",
    });
    dialogVisible.value = false;
    btnLoading.value = false;
    getTbaleData();
  } catch (error) {
    btnLoading.value = false;
    dialogVisible.value = false;
  }
};
// 导出
const onExport = async () => {
  const data = Object.assign(param(), { export: 1 });
  const res = await nftFilesApi(data);

  let head = {
    id: "ID",
    uid: "用户UID",
    nft_id: "NFT ID",
    image: "图片",
    video_file: "视频文件",
    zip_file: "压缩包文件",
    audio_file: "音频文件",
    request_id: "请求标识",
    task_id: "直播TaskID",
    is_sync: "直播是否同步",
    is_delete: "是否删除",
    live_status: "直播上架状态",
    priority: "排序",
  };
  exportToExcel(res.data.results, head, "Moby-NFT发行文件");
};

// 条数
const handleSizeChange = (number) => {
  pageable.pageSize = number;
  getTbaleData();
};

// 页码
const handleCurrentChange = (number) => {
  pageable.pageNum = number;
  getTbaleData();
};
onMounted(() => {
  getTbaleData();
});
</script>
